import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  isAuthenticated: false,
  user: {},
}
const getters = {
  isAuthenticated: state => state.isAuthenticated,
  user: state => state.user,
}
const mutations = {
  SET_IS_AUTHENTICATED(state, isAuthenticated) {
    if (isAuthenticated) {
      state.isAuthenticated = isAuthenticated;
    } else {
      state.isAuthenticated = false;
    }
  },
  SET_USER(state, user) {
    if (user) {
      state.user = user;
    } else {
      state.user = {}
    }
  }
}
const actions = {
  SetIsAuthenticated(state, isAuthenticated) {
    state.commit('SET_IS_AUTHENTICATED', isAuthenticated);
  },
  SetUser(state, user) {
    state.commit('SET_USER', user);
  },
  // 注销使用
  ClearState(state) {
    state.commit('SET_IS_AUTHENTICATED', false);
    state.commit('SET_USER', null);
  }
}


export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
})